package com.yunhui.service.impl;

import com.yunhui.mapper.CommentMapper;
import com.yunhui.entity.Comment;
import com.yunhui.service.CommentService;

import com.yunhui.util.RedisUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类  开启缓存注解
 * </p>
 *
 * @author WGH
 * @since 2019-12-15
 */

@Service
@CacheConfig(cacheNames="comment")
public class CommentServiceImpl implements CommentService{

    //注入评论的dao层
    @Resource
    private CommentMapper commentMapper;

    @Resource
    private RedisUtil redisUtil;


    /**
     * 根据条件查询分页
     * @param index 当前页
     * @param pageSize  每一页显示的数量
     * @param mid   商家id
     * @param rid   行程id
     * @return
     */

    @Override
    public List<Comment> finAllPage(int index,int pageSize,int mid,int rid) {
        List<Comment> list = null;

            int num = (index-1)*pageSize;

        try{
            list = commentMapper.finAllPage(num,pageSize,mid,rid);
        }catch (Exception e){
            e.printStackTrace();
        }

        return list;


    }

    /**
     * 执行添加评论功能
     * @param comment    添加
     * @return
     */
    @Override
    public boolean add(Comment comment) {
        int  num =0;
        try{
            num = commentMapper.insert(comment);
            Set<String> key =redisUtil.keys("comment*");
         //   System.err.println(key.toString());
            redisUtil.delte(key);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num>0;
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @Override
    public boolean deleteComment(Integer id) {
        int  num =0;
        try{
            num = commentMapper.deleteById(id);
            Set<String> key =redisUtil.keys("comment*");
           // System.err.println(key.toString());
            redisUtil.delte(key);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num>0;

    }

    /**
     * 根据id查询
     * @param rid
     * @return
     */
    @Override
    public List<Comment> selectRid(Integer rid) {
        return null;
    }

    /**
     * 根据时间查询未实现
     * @param date
     * @return
     */
    @Override
    public List<Comment> selectTime(Date date) {
        return null;
    }

    @Override
   
    public int selectCount(int mid,int rid) {

        int num = 0;
        try{
            num = commentMapper.selectCount(mid,rid);
        }catch (Exception e){e.printStackTrace();}

        return num;
    }

    @Override
    public int updataComment(Comment comment) {
        int num  = 0;
        try{
            num =commentMapper.updateById(comment);
            Set<String> key =redisUtil.keys("comment*");
            System.err.println(key.toString());
            redisUtil.delte(key);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 用户查询评论列表
     * @param uid   用户编号
     * @param context
     * @return
     */
    @Override
    public List<Comment> selectByuId(int index,int pageSize,int uid,String context) {
        List<Comment> list = null;
        int num = (index-1)*pageSize;
        try{
            list = commentMapper.selectByuId(num,pageSize,uid,context);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     *  根据用户查询评论的总页数
     * @param uid
     * @param context
     * @return
     */
    @Override
    public int selectCountByUser(int uid, String context) {
       int num = 0;
       try{
           num = commentMapper.selectCountByUser(uid,context);
       }catch (Exception e){
           e.printStackTrace();
       }
       return num ;
    }

    /**
     * @param index 当前页
     * @param pageSize 分页总数
     * @param jid 根据行程id查询分页
     * @return
     */
    @Override
    public List<Comment> selectByJid(int index, int pageSize, String jid) {
        List<Comment> list = null;

        int num = (index-1)*pageSize;
        try {
            list = commentMapper.selectByJid(num,pageSize,jid);
            System.out.println("从数据库中查找数据。。。。。。。。。。。。。。再添加到缓存中");
        }catch (Exception e){
            e.printStackTrace();
        }
      //  System.out.println(list);
        return list;
    }


    @Override
    public int selectCountByJid(String jId) {
        int num = 0;
        try{
            num = commentMapper.selectCountByJid(jId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num ;
    }

}
